package com.layui.ad.controller;

import com.layui.ad.annotation.Log;
import com.layui.ad.entity.SysMenu;
import com.layui.ad.enums.BusinessType;
import com.layui.ad.query.BaseQuery;
import com.layui.ad.query.MenuQuery;
import com.layui.ad.service.SysMenuService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.vo.SysMenuVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Resource
    private SysMenuService sysMenuService;

    public void init(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("basecontroller 被调用");
        System.out.println(request.getMethod());
    }

//    @ResponseBody
//    @RequestMapping("/menuList")
//    public JsonResult menuList() {
//        List<SysMenuVo> menuList = sysMenuService.getMenuList();
////        System.out.println(menuList);
//        return JsonResult.ok(menuList);
//    }
//
//    @ResponseBody
//    @RequestMapping("/menutree")
//    public JsonResult menuTree() {
//        List<SysMenuVo> menuList = sysMenuService.menuTree();
//        return JsonResult.ok(menuList);
//    }
//
    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
//        request.setAttribute("con", "menu");
//        request.setAttribute("act", "index");
        return "menu/index";
    }

    @ResponseBody
    @PostMapping("/add")
    public JsonResult add(@RequestBody SysMenu entity) {
        return sysMenuService.edit(entity);
    }



    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(MenuQuery query) {
        return sysMenuService.getList(query);
    }

    /**
     * 获取记录详情
     *
     * @param id    记录ID
     * @param model 模型
     * @return
     */

    @GetMapping("/edit")
    public String edit(HttpServletRequest request,Integer id) {
        Map<String, Object> info = new HashMap<>();

        String pid = request.getParameter("pid")!=null?request.getParameter("pid"):"0";
        info.put("pid",Integer.parseInt(pid));
        if (id != null && id > 0) {
            info = sysMenuService.findByPk(id);
        }
        List<Map<String, Object>> menuList = sysMenuService.getMenuByPid(0);
        List<Integer> typeList = sysMenuService.getTypeList(id);
        request.setAttribute("menuList", menuList);
        request.setAttribute("info", info);
        request.setAttribute("typeList",typeList);
        return "menu/edit";
    }
    @ResponseBody
    @PostMapping("/update")
    public JsonResult update(@RequestBody SysMenu entity) {
        return sysMenuService.edit(entity);
    }

    @ResponseBody
    @GetMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id,HttpServletRequest request){
        return sysMenuService.deleteById(id);
    }

    @ResponseBody
    @PostMapping("/setStatus")
    @Log(title = "菜单",businessType = BusinessType.STATUS)
    public JsonResult setStatus(@RequestBody SysMenu menuVo){
        return sysMenuService.setStatus(menuVo);
    }
//
//    @RequestMapping("/edit")
//    public String edit(HttpServletRequest request, HttpServletResponse response) {
//        String pid = request.getParameter("pid");
//        Integer parentId = 0;
//        if (pid != null) {
//            parentId = Integer.parseInt(pid);
//        }
//        //id
//        Integer id = 0;
//        String strId = request.getParameter("id");
//        if (strId != null) {
//            id = Integer.parseInt(strId);
//        }
//
//        SysMenu data = sysMenuService.findByPk(id);
//        if (data != null) {
//            parentId = data.getParentId();
//        }
//        System.out.println("信息" + parentId);
//        request.setAttribute("parentId", parentId);
//        request.setAttribute("con", "menu");
//        request.setAttribute("act", "doadd");
//        request.setAttribute("data", data);
//        List<Map<String, Object>> menuList = sysMenuService.getMenuByPid(0);
//        request.setAttribute("menuList", menuList);
//        return "menu/add";
//    }
//
//    @RequestMapping("/update")
//    @ResponseBody
//    public JsonResult doadd(SysMenu sysMenu) {
//        Integer id = sysMenu.getId();
//        System.out.println(id);
////        if (sysMenu.getIsPublic()==null){
////            sysMenu.setIsPublic(0);
////        }
////        if (sysMenu.getIsShow()==null){
////            sysMenu.setIsShow(0);
////        }
//
//        if (0 != id) {
//            sysMenuService.updateToDb(sysMenu);
//        } else {
//            sysMenuService.saveToDb(sysMenu);
//        }
//        return JsonResult.ok();
//    }
//
//    @RequestMapping("getSysIcon")
//    public String getSysIcon(HttpServletRequest request){
//        request.setAttribute("con", "menu");
//        request.setAttribute("act", "getSysIcon");
//        return  "menu/icon";
//    }
//
//    @RequestMapping("/drop")
//    @ResponseBody
//    public JsonResult drop(Integer id){
//        boolean flag = sysMenuService.drop(id);
//        if(flag){
//            return JsonResult.ok();
//        }else{
//            return JsonResult.fail("请先删除其子类");
//        }
//    }


}
